/*
 * @lc app=leetcode.cn id=515 lang=javascript
 *
 * [515] 在每个树行中找最大值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var largestValues = function (root) {
  let res = []
  let queque = []
  if (root===null) {
    return res
  }
  queque.push(root)
  while (queque.length) {
    const len = queque.length
    let curLevelMax=0
    for (let i = 0; i < len; i++){
      const levelCur = queque.shift()
      curLevelMax=(curLevelMax>levelCur.val?curLevelMax:levelCur.val)
      levelCur.left&&queque.push(levelCur.left)
      levelCur.right&&queque.push(levelCur.right)
    }
    res.push(curLevelMax)
  }
  return res
};
// @lc code=end

